package com.hoopchina.ui.search;

// import class
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

/**
 * SearchHistoryDatabaseHelper
 * 用于搜索历史记录创建和使用数据库
 * 
 * @author Guo Baiyu
 */
public class SearchHistoryDatabaseHelper extends SQLiteOpenHelper {

    public SearchHistoryDatabaseHelper( Context context, String name, CursorFactory factory,
            int version ) {
        super( context, name, factory, version );
    }

    public SearchHistoryDatabaseHelper( Context context, String name ) {
        this( context, name, 3 );
    }

    public SearchHistoryDatabaseHelper( Context context, String name, int version ) {
        this( context, name, null, version );
    }

    /*
     * 关闭数据库
     * @see android.database.sqlite.SQLiteOpenHelper#close()
     */
    @Override
    public synchronized void close() {
        super.close();
    }

    /*
     * 打开数据库
     * @see android.database.sqlite.SQLiteOpenHelper#onOpen(android.database.sqlite.SQLiteDatabase)
     */
    @Override
    public void onOpen( SQLiteDatabase db ) {
        super.onOpen( db );
    }

    /*
     * 创建或打开数据库
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
     */
    @Override
    public void onCreate( SQLiteDatabase db ) {
        System.out.println( "Create a database." );
        String len  = SearchBar.SEARCHBAR_MAX_LEN + 2 + "";
        try {
            db.execSQL( "create table search_history_table(id int , search_record varchar("+len+"));" );

        }
        catch ( SQLException e ) {
            e.printStackTrace();
        }
        // db.execSQL("create table search_history_table(id INTEGER PRIMARY KEY AUTOINCREMENT, search_record VARCHAR(20) NOT NULL);");
    }

    /*
     * 更新数据库
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase,
     * int, int)
     */
    @Override
    public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
        System.out.println( "Updata a database." );
    }

}
